package com.xing.book.dao.impl;

import com.xing.book.dao.BookDao;
import com.xing.book.domin.Book;
import com.xing.book.util.DbUtils;
import com.xing.book.util.DbUtils_druid;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class BookDaoImpl implements BookDao {
    @Override
    public List<Book> selectAll() {
      /*  Connection connection= DbUtils.getConnection();
        PreparedStatement pstat=null;
        ResultSet rs=null;

        try {
             pstat = connection.prepareStatement("select * from book");
             rs = pstat.executeQuery();

             List<Book> books=new ArrayList<>();
            while (rs.next()){
                int id=rs.getInt("id");
                String title=rs.getString("title");
                String author=rs.getString("author");
                Date publicDate=rs.getTimestamp("publicDate");
                String publisher=rs.getString("publisher");
                String isbn=rs.getString("isbn");
                BigDecimal price=rs.getBigDecimal("price");
                String picture=rs.getString("picture");
                int cid=rs.getInt("cid");

                Book book=new Book(id,title,author,publicDate,publisher,isbn,price,picture,cid);
                books.add(book);
            }
            return books;

        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            DbUtils.closeAll(rs,pstat,connection);
        }*/
        String sql="select * from book";
        return DbUtils_druid.selectList(sql,Book.class);

    }

    @Override
    public List<Book> selectByTitle(String title) {

        /*Connection connection= DbUtils.getConnection();
        PreparedStatement pstat=null;
        ResultSet rs=null;

        try {
            pstat = connection.prepareStatement();
            pstat.setString(1,"%"+title+"%");
            rs = pstat.executeQuery();

            List<Book> books=new ArrayList<>();
            while (rs.next()){
                int id=rs.getInt("id");
                String tit=rs.getString("title");
                String author=rs.getString("author");
                Date publicDate=rs.getTimestamp("publicDate");
                String publisher=rs.getString("publisher");
                String isbn=rs.getString("isbn");
                BigDecimal price=rs.getBigDecimal("price");
                String picture=rs.getString("picture");
                int cid=rs.getInt("cid");

                Book book=new Book(id,tit,author,publicDate,publisher,isbn,price,picture,cid);
                books.add(book);
            }
            return books;

        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            DbUtils.closeAll(rs,pstat,connection);
        }*/

        String sql="select id,title,author,publicDate,publisher,isbn,price,picture,cid from book where title like ?";
        return DbUtils_druid.selectList(sql,Book.class,"%"+title+"%");
    }

    @Override
    public void insert(Book book) {
        String sql="insert into book(title,author,publicDate,publisher,isbn,price,picture,cid) values (?,?,?,?,?,?,?,?);";
        Object[] params={book.getTitle(),book.getAuthor(),book.getPublicDate(),book.getPublisher(),book.getIsbn(),book.getPrice(),book.getPicture(),book.getCid()};
        DbUtils_druid.executeUpdate(sql,params);


    }

    @Override
    public Book selectById(int bookId) {
        /*Connection connection= DbUtils.getConnection();
        PreparedStatement pstat=null;
        ResultSet rs=null;

        try {
            pstat = connection.prepareStatement("select id,title,author,publicDate,publisher,isbn,price,picture,cid  from book where id= ?");
            pstat.setInt(1,bookId);
            rs = pstat.executeQuery();

            Book book=null;
            if (rs.next()){
                int id=rs.getInt("id");
                String tit=rs.getString("title");
                String author=rs.getString("author");
                Date publicDate=rs.getTimestamp("publicDate");
                String publisher=rs.getString("publisher");
                String isbn=rs.getString("isbn");
                BigDecimal price=rs.getBigDecimal("price");
                String picture=rs.getString("picture");
                int cid=rs.getInt("cid");

                book=new Book(id,tit,author,publicDate,publisher,isbn,price,picture,cid);

            }
            return book;

        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            DbUtils.closeAll(rs,pstat,connection);
        }*/
        String sql="select id,title,author,publicDate,publisher,isbn,price,picture,cid  from book where id=?";

        List<Book> books = DbUtils_druid.selectList(sql, Book.class, bookId);
        if(books!=null&&books.size()>0){
            return books.get(0);
        }
        return null;
    }

    @Override
    public void update(Book book) {
        String sql="update book set title=?,author=?,publicDate=?,publisher=?,isbn=?,price=?,picture=?,cid=? where id=?";
        Object[] params={book.getTitle(),book.getAuthor(),book.getPublicDate(),book.getPublisher(),book.getIsbn(),book.getPrice(),book.getPicture(),book.getCid(),book.getId()};
        DbUtils_druid.executeUpdate(sql,params);
    }

    @Override
    public void delete(int bookId) {
        String sql="delete from book where id=?";
        DbUtils_druid.executeUpdate(sql,bookId);
    }


}
